package com.hzsk.lisence.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.myexception.BaseException;
import com.hzsk.lisence.entity.EmUkeyEntity;
import com.hzsk.lisence.entity.HardwareEntity;
import com.hzsk.lisence.entity.request.UkeyRequest;
import com.hzsk.lisence.mapper.UkeyMapper;
import com.hzsk.lisence.service.EmUkeyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @Description :
 * @Date : 2024/8/13
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Service
public class EmUkeyServiceImpl implements EmUkeyService {
    
    @Resource
    private UkeyMapper ukeyMapper;

    @Override
    public PageInfo<EmUkeyEntity> getUkeyPage(UkeyRequest ukeyRequest) {
        PageHelper.startPage(ukeyRequest.getPageNum(),ukeyRequest.getPageSize());
        List<EmUkeyEntity> emUkeyEntityList = ukeyMapper.selectUkeyPage(ukeyRequest);
        return new PageInfo<>(emUkeyEntityList);
    }

    @Override
    @Transactional
    public String generatePassword(String code,String userAccount) throws Exception {
        //新增U盾制作记录
        EmUkeyEntity ukeyEntity = new EmUkeyEntity();
        ukeyEntity.setCode(code);
        ukeyEntity.setOperator(userAccount);
        String password = null;
        List<EmUkeyEntity> emUkeyEntityList = ukeyMapper.selectUkeyCountByCode(ukeyEntity);
        if (!CollectionUtils.isEmpty(emUkeyEntityList)){
            if (emUkeyEntityList.get(0).getStatus()==1){
                throw new BaseException("该U盾已制作成功,请勿重新制作");
            }
            ukeyMapper.updateUkeyOperator(ukeyEntity);
            password = emUkeyEntityList.get(0).getPassword();
        }else {
            UUID uuid = UUID.randomUUID();
            password = uuid.toString().replace("-","");
            ukeyEntity.setPassword(password);
            ukeyMapper.createUkey(ukeyEntity);
        }
        return password;
    }

    @Override
    public String getPassword(String code) throws Exception {
        EmUkeyEntity ukeyEntity = new EmUkeyEntity();
        ukeyEntity.setCode(code);
        List<EmUkeyEntity> emUkeyEntityList = ukeyMapper.selectUkeyCountByCode(ukeyEntity);
        if (!CollectionUtils.isEmpty(emUkeyEntityList)){
            return emUkeyEntityList.get(0).getPassword();
        }
        return null;
    }

    @Override
    @Transactional
    public void updateStatus(String code, Integer status) throws Exception {
        EmUkeyEntity ukeyEntity = new EmUkeyEntity();
        ukeyEntity.setCode(code);
        ukeyEntity.setStatus(status);
        List<EmUkeyEntity> emUkeyEntityList = ukeyMapper.selectUkeyCountByCode(ukeyEntity);
        if (CollectionUtils.isEmpty(emUkeyEntityList)){
            throw new Exception("硬件标识号不存在,变更状态失败");
        }
        ukeyMapper.updateUkeyStatus(ukeyEntity);
    }
}
